/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.network.request.builder.LoginRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.LoginResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.LoginResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.umeng.analytics.MobclickAgent;

/**
 * @author wdy
 * 
 */
public class LoginActivity extends BaseActivity implements OnClickListener,
		OnResponseListener {
	private Context mContext;
	private EditText mId;
	private EditText mPwd;
	
	private LoginRequestBuilder mReq = new LoginRequestBuilder();
	private LoginResponseHandler mHandler = new LoginResponseHandler();
	
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_launch);
		mContext = this;

		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mId = (EditText) findViewById(R.id.login_id);
		mPwd = (EditText) findViewById(R.id.login_pwd);
	}

	public void reg() {
		findViewById(R.id.login_login).setOnClickListener(this);
		findViewById(R.id.login_reg).setOnClickListener(this);
		findViewById(R.id.login_forget).setOnClickListener(this);
	}

	public void initData(Bundle savedInstanceState) {
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.login_login:
			if(validInput()){
				mReq.setId(mId.getText().toString()).setPwd(mPwd.getText().toString()).postJsonRequest(mContext, mHandler);
			}
			break;
		case R.id.login_reg:
			break;
		case R.id.login_forget:
			break;
		default:
			break;
		}

	}
	
	private boolean validInput(){
		if(mId.getText().toString().length()==0||mPwd.getText().toString().length()==0){
			Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		LoginResponse response = (LoginResponse) classz.getResponse();
		// insert db
	}

	@Override
	public void onError(IResponseHandler classz) {
		
	}

	/**
	 * umeng
	 */
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this); // 统计时长
	}

	/**
	 * umeng
	 */
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
